<template>
	<view>
		<cu-custom bgColor="bg-white" :isBack="true">
			<block slot="backText"></block>
			<block slot="content">积分转赠</block>
		</cu-custom>

		<view class="qui-cells solid-top">
			<view class="qui-cell" @tap="showModal" data-target="Modal">
				<view class="cell-hd fs26 fw600">
					转赠给
				</view>
				<view class="cell-bd fs26 fw600 tr">
					{{bedonateNickname}}
				</view>
				<view class="arrow-r"></view>
			</view>
		</view>
		<view class="qui-cells mt10">
			<view class="qui-cell border_none">
				<view class="cell-hd fs26 fw600">
					转赠数量
				</view>
			</view>
			<view class="qui-cell border_none pt0" style="padding-bottom: 20rpx;">
				<view class="flex radius10" style="background-color: #f7f7f7;padding: 20rpx;width: 100%;">
					<view class="fs36 fw600 mr10"></view>
					<input class="fs38 mt5 ff" v-model="money" @blur="caculate" placeholder="输入转赠数量" name="input" placeholder-class="fs30 fw600 text-muted"></input>
				</view>
			</view>
			<view class="qui-cell pt0" style="padding-bottom: 20rpx;">
				<view class="cell-hd fs22 text-gray">
					可用积分{{UserBalance.LiveMoney}}
				</view>
				<view class="cell-hd fs22 text-red" v-if="fee>0">
					手续费{{fee}}积分
				</view>
				<view class="cell-bd fs24 text-orange tr" @tap="donateAll">
					全部
				</view>
			</view>
			
		</view>
		<view class="qui-cell border_none checkbox-radius">
			<checkbox @tap="agree"></checkbox>
			<view class="fs26 text-muted flex ml20">我已阅读并理解<navigator class="text-blue">《转赠服务协议》</navigator>
			</view>
		</view>
		<view class="cu-modal" :class="modalName=='Modal'?'show':''">
			<view class="cu-dialog" style="width: 100%;">
				<view class="cu-bar bg-white">
					<view class="fs30 fw600 ml30">转赠给</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-gray"></text>
					</view>
				</view>
				<view class="cu-bar search bg-white">
					<view class="search-form searchbar radius10 tl plr20" style="background-color:#f7f7f7;height: 90rpx;">
						<input  v-model="searchkey" :adjust-position="false" type="text" placeholder="请输入会员ID/手机号码"
						 confirm-type="search"></input>
						<image :src="imgurl+'/icon_search.png'" @tap="GetUser"></image>
					</view>
				</view>
					<view v-if="UserList.length<=0 && isSearch">
						<text style="text-align: center;">没有任何记录</text>
						</view>
				<view v-if="UserList.length>0 ">
					<scroll-view scroll-y="true" style="height:500rpx;">
						<label class="cu-form-group" v-for="(item,index) in UserList" @tap="activeIndex = index;beDonateUserID=item.userID;bedonateNickname=item.nickName">
							<view class="flex align-center pt20 pb20">
								<view>
									<image :src="item.headIMg" class="img85 mr30 round"></image>
								</view>
								<view style="text-align: left;">
									<view class="fs28 fw600">{{item.nickName}}</view>
									<view class="flex align-center">
										<image :src="imgurl+'fx_img3.png'" mode="" class="img30"></image>
										<text class="text-muted fs22">{{item.phone}}</text>
									</view>
									<view class="fs22 text-muted">
										<text class="mr5">ID</text>{{item.userID}}
									</view>
								</view>
							</view>
							<view v-show="activeIndex == index">
								<image :src="imgurl+'/icon_check.png'" class="img36"></image>
							</view>
						</label>
					</scroll-view>
					
					
				</view>
				
				<view v-else class="bg-white flex justify-center align-center" style="height:500rpx;">
					
					<image :src="imgurl+'zz_2.png'" mode="" class="zz_2"></image>
				</view>
			</view>
		</view>

		<view  class="uni-btns text-bold" v-if="isopen">
			<button class="uni-btn-main radio uni-btn fs30" @tap="DonateHandle">确定转赠</button>
		</view>
		<view  class="uni-btns text-bold"  v-if="!isopen">
			<button class="uni-btn-main radio uni-btn fs30" >还没开启转赠功能</button>
		</view>
	</view>
</template>

<script>
	import base from 'utils/base.js';
	import tools from 'utils/tools.js';
	export default {
		data() {
			return {
				isopen:false,
				bedonateNickname:'',
				searchkey:'',
				donateSet:{},
				activeIndex: 0,
				modalName: '',
				imgurl: this.$imgurl,
			    UserList:[],
				isSearch:false,
				money:0,
				fee:0,
				summit:false,
				beDonateUserID:0,
				UserBalance: { LiveMoney: 0 },
				agree1:false
			}
		},
		mounted() {
	
			this.GetDonateSet();
			this.GetUserBalance();
		},
		methods: {
			agree:function(){
				this.agree1=!this.agree1;
			},
			donateAll:function(){
				let currentPage = this;
				if (currentPage.donateSet.ServiceChargeState==1 && currentPage.donateSet.ServiceCharge>0)
				   {
				    var livemoney=currentPage.UserBalance.LiveMoney;
				    let donatemoney=Math.ceil(livemoney/(1+currentPage.donateSet.ServiceCharge));
				    let koumoney=livemoney-donatemoney;
				    currentPage.money=donatemoney;
				    currentPage.fee=koumoney;
				  }
				
				
			},
			GetDonateSet: function() {
				let currentPage = this;
			
				let params = {
					type: 'GET',
					url: '/api/Donate/GetDonateSet',
					data: {type:2},
					sCallback: function(response) {
						if (response.IsSuccess) {
						
						 currentPage.donateSet = response.Data;
						 if(response.Data!=null ) currentPage.isopen=true;
						} else {
							uni.showToast({
								title: response.Msg
							});
						}
					}
				};
				base.request(params);
			},
			caculate:function(){
				let currentPage = this;
				 if (currentPage.donateSet.ServiceChargeState==1 && currentPage.donateSet.ServiceCharge>0)
				    {
				      currentPage.fee = Math.ceil(currentPage.money * currentPage.donateSet.ServiceCharge);
				   }
				  
				  if(currentPage.fee+parseInt(currentPage.money)>currentPage.UserBalance.LiveMoney)
				  {
					  currentPage.donateAll();
				  }
			},
			
			GetUserBalance: function() {
				let currentPage = this;
			
				let params = {
					hideLoading: true,
					type: 'GET',
					url: '/api/UserInfo/GetUserBalance',
					data: { typeId: 2 },
					sCallback: function(response) {
						if (response.IsSuccess) {
							let userBalance = response.Data;
							currentPage.UserBalance = userBalance;
						} else {
							uni.showToast({
								title: response.Msg
							});
						}
					}
				};
				base.request(params);
			},
			DonateHandle:function(){
				let currentPage = this;
				let globalData = getApp().globalData;
				if(!currentPage.agree1)
				{
					tools.showToast('请认真阅读转赠协议并且同意');
					return;
				}
				if(currentPage.summit)
				{
					tools.showToast('请勿重复提交');
					return;
				}
				if(currentPage.beDonateUserID<=0)
				{
					tools.showToast('请选择要转赠的人');
					return;
				}
				if(currentPage.beDonateUserID==globalData.UserInfo.UserID)
				{
					tools.showToast('不能转赠给自己');
					return;
				}
				if(currentPage.money<=0 || !tools.IsMoney(currentPage.money))
				{
					tools.showToast('请输入转赠金额');
					return;
				}
				
				if(currentPage.money>currentPage.UserBalance.LiveMoney)
				{
					tools.showToast('转赠余额不足');
					return;
				}
				 currentPage.summit=true;
				let params = {
					type: 'POST',
					url: '/api/UserInfo/DonateHandle',
					data: {Type:2,Value:currentPage.money,BeDonateUserID:currentPage.beDonateUserID},
					sCallback: function(response) {
						 currentPage.summit=false;
						if (response.IsSuccess &&  response.Data=='') {
							uni.navigateTo({
							    url: '/pages/my/pointsdonateSuccess?donatePoints='+currentPage.money
							});
						} else {
												tools.showToast(response.Data);
							
						}
					}
				};
				base.request(params);
			},
			GetUser: function() {
				let currentPage = this;
			    let searchkey=currentPage.searchkey;
				let userID=0;
				let phone="";
				
				if(searchkey.length<=9) userID=searchkey;
				else phone=searchkey;
				let params = {
					type: 'POST',
					url: '/api/UserInfo/UserList',
					data: {UserID:userID,Phone:phone,PIndex:1,PageSize:10},
					sCallback: function(response) {
						currentPage.isSearch=true;
						if (response.IsSuccess) {
							if(response.Data!=null && response.Data.item1!=null)
						 currentPage.UserList = response.Data.item1;
						 currentPage.beDonateUserID=response.Data.item1[0].userID;
						 currentPage.bedonateNickname=response.Data.item1[0].nickName;
						} else {
							uni.showToast({
								title: response.Msg
							});
						}
					}
				};
				base.request(params);
			},
			showModal(e) {
				this.modalName = 'Modal'
			},
			hideModal(e) {
				this.modalName = null
			},
			radioChange: function(evt) {
				for (let i = 0; i < this.items.length; i++) {
					if (this.items[i].value === evt.target.value) {
						this.current = i;
						break;
					}
				}
			}
		},


	}
</script>

<style lang="scss" scoped>
	.zz_2 {
		width: 300rpx;
		height: 300rpx;
	}
	.search {
	
		image {
			width: 34upx;
			height: 34upx;
			display: inline-block;
			margin-right: 10upx;
		}
	
	}
	.cu-dialog{
		bottom: 0;
		    vertical-align: bottom;
			border-radius: 0;
	}
	.uni-btn-main {
		background-color: #ff5e1f !important;
	}

	.img24 {
		width: 24rpx;
		height: 24rpx;
		position: relative;
		top: 5rpx;
	}

	.img85 {
		width: 85rpx;
		height: 85rpx;
	}

	.img30 {
		width: 30rpx;
		height: 30rpx;
	}

	.img36 {
		width: 36rpx;
		height: 36rpx;
	}
</style>
